package com.woniuxy.boot.nb_auth.util;

import java.io.IOException;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.woniuxy.boot.nb_auth.pojo.RealNamePojo;
import okhttp3.Call;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import org.springframework.stereotype.Component;

@Component
public class RealName {
    public static RealNamePojo postData( String name, String idNo) throws IOException {
        String result = "";
        RequestBody formBody = new FormBody.Builder().add("name", name).add("idNo", idNo).build();
        Request request = new Request.Builder().url("https://idenauthen.market.alicloudapi.com/idenAuthentication").addHeader("Authorization", "APPCODE " + "f6ae13168243454b8f648dc552906b94").post(formBody).build();

        Call call = new OkHttpClient().newCall(request);
        Response response = null;
        try {
            response = call.execute();
        } catch (IOException e) {
            System.out.println("execute failed, message:" + e.getMessage());
        }
        assert response != null;
        if (!response.isSuccessful()) {      // 当返回结果发生错误时
            // 状态码为403时一般是套餐包用尽，需续购；注意：续购不会改变秘钥（appCode），仅增加次数
            // 续购链接：https://market.aliyun.com/products/57000002/cmapi025518.html
            System.out.println("request failed----" + "返回状态码" + response.code()  + ",message:" + response.message());
        }
        result = response.body().string();//此处不可以使用toString()方法，该方法已过期

        RealNamePojo realNamePojo=new RealNamePojo();
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            realNamePojo = objectMapper.readValue(result, RealNamePojo.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return realNamePojo;
    }
}